{include file="public/header" /}
<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="ibox-title">
        <h5>添加消息</h5>
        <div class="ibox-tools">
            <a class="collapse-link">
                <i class="fa fa-chevron-up"></i>
            </a>
        </div>
    </div>
    <div class="ibox-content layui-form">
        <div class="layui-row layui-col-space10 layui-form-item ">
            <div class="layui-form-item layui-col-xs-offset1 layui-col-xs8">
                <label class="layui-form-label">标题：</label>
                <div class="layui-input-block">
                    <input type="text" class="layui-input" name="n_title" lay-verify="required" placeholder="消息标题">
                </div>
            </div>
            <div class="layui-form-item layui-col-xs-offset1 layui-col-xs8">
                <label class="layui-form-label">类型：</label>
                <div class="layui-input-block">
                    <select name="n_type" lay-verify="required" lay-search="" id="type" lay-filter="type">
                        <option value="">请选择消息类型</option>
                        <option value="1">系统消息</option>
                        <option value="2">个人消息</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-col-xs-offset1 layui-col-xs8">
                <label class="layui-form-label">接收端：</label>
                <div class="layui-input-block">
                    <select name="n_app" lay-verify="required" lay-search="" id="user" lay-filter="user">
                        <option value="">请选择接收端</option>
                        <option value="1">用户端</option>
                        <option value="2">商户端</option>
                        <option value="3">业务端</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-col-xs-offset1 layui-col-xs8">
                <label class="layui-form-label">接收人：</label>
                <div class="layui-input-block">
                    <select name="n_user_id" lay-verify="required" id="people" lay-search="" lay-filter="people">
                        <option value="">请选择接收人</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-col-xs-offset1 layui-col-xs8">
                <label class="layui-form-label">内容：</label>
                <div class="layui-input-block">
                    <textarea id="n_content" lay-verify="content"></textarea>
                </div>
            </div>
            <div class="layui-form-item layui-col-xs-offset1 layui-col-xs8">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" lay-filter="component-form-element">保存
                    </button>
                    <button class="layui-btn layui-btn-primary" onclick="history.go(-1)">返回</button>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer" /}
<script>
    //百度富文本编辑器
    var editor = UE.getEditor('n_content', {
        initialFrameHeight:450,
        autoHeight: false,
        autoHeightEnabled:false,
        autoFloatEnabled:false
    });

    layui.use(['form'], function() {
        var form = layui.form;
        form.on('select(type)', function(data){
            if(data.value && $('#user').val()){
                getUser(data.value,$('#user').val());
            }
        });
        form.on('select(user)', function(data){
            if(data.value && $('#type').val()){
                getUser($('#type').val(),data.value);
            }
        });
        function getUser(type,user){
            if(type == 1){
                switch(user){
                    case '1':
                        $("#people").empty();
                        $('#people').append('<option value="">请选择接收人</option><option value="all">全体用户</option>');
                        break;
                    case '2':
                        $("#people").empty();
                        $('#people').append('<option value="">请选择接收人</option><option value="all">全体商户</option>');
                        break;
                    case '3':
                        $("#people").empty();
                        $('#people').append('<option value="">请选择接收人</option><option value="all">全体业务员</option>');
                        break;
                }
                form.render('select');
            }else{
                $.ajax({
                    url:"{:url('userData')}",
                    dataType:"json",
                    data:'type='+type +'&user=' + user,
                    type:'post',
                    success:function(res){
                        var opt = null;
                        $.each(res.msg,function(key,vo){
                            opt = opt+"<option value="+vo.id+">"+vo.name+"</option>";
                        })
                        $("#people").empty();
                        $('#people').append('<option value="">请选择接收人</option>' + opt);
                        form.render('select');
                    }
                })
            }
        }
        form.verify({
            content:function(value, item){
                if(editor.getContent() == ''){
                    return '内容不能为空';
                }
            }
        })
        form.on('submit(component-form-element)', function (data) {
            $('.layui-btn').addClass('layui-disabled').attr('disabled','disabled');
            data.field.n_content = editor.getContent()
            $.ajax({
                url:"{:url('noticeAdd')}",
                type:'post',
                dataType:'json',
                data:data.field,
                success:function(res){
                    if (res.code == 200) {
                        layer.msg(res.msg,{icon:1,time:1500,shade:0.1},function(index){
                            wk.layer_close();
                        })
                    } else {
                        $(".layui-btn").removeClass('layui-disabled').removeAttr('disabled');
                        wk.error(res.msg);
                        return false;
                    }
                }
            })
        });
    });
</script>
</body>
</html>